<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>车管所管理列表</title>
    <link href="../../layui/css/layui.css" rel="stylesheet">
    <link href="../../static/css/style.css" rel="stylesheet">
    <script src="../../static/js/jquery.min.js"></script>
    <script src="../../static/js/base.js"></script>
    <script src="../../static/js/ax.js"></script>
    <script src="../../static/js/jquery.cookie.js"></script>
    <script src="../../static/js/avalon.js"></script>
    <!--[if lt IE 9]>
    <script src="../../static/js/html5.js"></script>
    <script src="../../static/js/respond.js"></script>
    <![endif]-->
</head>

<body>

    <!--主体内容-->
    <div class="layui-fluid">
        <div class="layui-card content-wrap">

            <form class="layui-form layui-form-pane layui-form-item" action="">
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-sm4">
                        <label class="layui-form-label">省份</label>
                        <div class="layui-input-block">
                            <select id="provinceId" lay-filter="provinceSelect">
                                <option value=''>请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-sm4">
                        <label class="layui-form-label">市辖</label>
                        <div class="layui-input-block" id="parent_selet">
                            <select id="cityId" lay-filter="citySelect">
                                <option value=''>请选择</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-sm4">
                        <a class="layui-btn add_btn" data-type="reload" id="searchBtn"><i class="layui-icon layui-icon-search"></i>搜索</a>
                        <button class="layui-btn delete_btn" data-type="reloadClean">
                            <i class="layui-icon layui-icon-delete"></i>清空</button>
                        <!--  <button class="layui-btn add_btn" data-type="reload" id="searchBtn"><i class="layui-icon layui-icon-search"></i>搜索</button>-->

                    </div>
                </div>
            </form>
            <div class="layui-form-item">
                <div>
                    <button class="layui-btn add_btn ax-btn" id="add">
                        <i class="layui-icon layui-icon-add-circle"></i>新增</button>
                    <button class="layui-btn change_btn ax-btn" id="update" >
                        <i class="layui-icon layui-icon-edit"></i>编辑</button>
                    <!-- <button class="layui-btn delete_btn ax-btn" id="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>-->
                </div>
            </div>
            <table class="layui-hide" id="activeListTbale" lay-filter="active"></table>

        </div>
    </div>
    <!--主体内容-->

    <script src="../../layui/layui.js"></script>

</body>


<script>

    var table;
    var tableIns;
    var form;
    //获取省市区
    $(function () {
        ax.post({
            url: schoolWebUrl+'/api/captcha/getRegionProvince',
            contentType: "application/x-www-form-urlencoded",
            data: "",
            success: function (r) {
                for (var i = 0; i < r.data.length; i++) {
                    $("#provinceId").append(' <option value=' + r.data[i].code + '>' + r.data[i].name + '</option>');
                }
                //默认为第一个为北京市市
                ax.post({
                    url:  schoolWebUrl+'/api/captcha/getRegionCode',
                    contentType: "application/x-www-form-urlencoded",
                    data: { "code": 110000 },
                    success: function (r) {
                        for (var i = 0; i < r.data.length; i++) {
                            $("#cityId").append(' <option value=' + r.data[i].code + '>' + r.data[i].name + '</option>');
                        }
                        setTimeout(function () {
                            form.render('select');
                        },200)

                    },
                });
            },
        });
    });
    layui.use(['table', 'form', 'laydate', 'layer', 'element'], function () {
        table = layui.table;
        form = layui.form;
        var layer = layui.layer;

        tableIns = ax.VTable({
            elem: '#activeListTbale',
            url:  schoolWebUrl+'/api/division/list',
            autoPrefix: false,
            id: 'testReload',
            cols: [[{
                type: 'checkbox',
                fixed: 'left'
            }, {
                /* field: 'id',*/
                width: 80,
                title: '序号',
                fixed: 'left',
                type: 'numbers'
            }, {
                field: 'cityId',
                hide: true,
                width: 80,
                title: '城市ID',
                fixed: 'left'
            }, {
                field: 'pac',
                minWidth: 160,
                title: '省市',
            }, {
                field: 'name',
                minWidth: 160,
                title: '车管所名称',
            }, {
                field: 'username',
                width: 160,
                title: '创建人',
            }, {
                field: 'updated',
                width: 160,
                title: '修改时间',
                templet: '#stateTpl'
            }, {
                field: 'created',
                width: 160,
                title: '创建时间'
            }/*,
                {
                    fixed: 'right',
                    width: 160,
                    title: '操作',
                    toolbar: '#barDemo'
                }*/]],

        });


        form.on('select(provinceSelect)', function (data) {
            ax.post({
                url:  schoolWebUrl+'/api/captcha/getRegionCode',
                contentType: "application/x-www-form-urlencoded",
                data: { "code": data.value },
                success: function (r) {
                    $("#cityId").empty();

                    $("#cityId").append('<option value="">请选择</option>');
                    for (var i = 0; i < r.data.length; i++) {
                        $("#cityId").append(' <option value=' + r.data[i].code + '>' + r.data[i].name + '</option>');
                    }
                    layui.form.render('select');
                },
            });
        });


        var $ = layui.$, division = {
            reload: function () {
                var demoReload = $('#demoReload');

                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        provinceId: $("#provinceId").val(),
                        cityId: $("#cityId").val()
                    }
                });
            }
        };


        $('#delete').on('click', function () {
            //获取选中的数据
            layer.confirm('真的删除这行数据么?', function (index) {
                var checkStatus = table.checkStatus('testReload'), data = checkStatus.data;
                if (data.length > 1) {
                    layer.close(index);
                    layer.alert("最多选择一条数据");
                    return;
                }
                var postData = { "id": data[0].id };
                ax.post({
                    type: 'POST',
                    url:  schoolWebUrl+'/api/division/delete',
                    contentType: "application/x-www-form-urlencoded",
                    data: postData,
                    success: function (r) {
                        layer.alert(r.message);
                        reset();
                    },
                });
            });
        });
        //新增车管所
        $('#add').on('click', function () {
            top.layer.open({
                title: "新增车管所",
                type: 2,
                fixed: true, //不固定
                maxmin: true,
                area: ['80%', '80%'],
                content: './aos/division/divisionAdd.html'
            });
        });
        //修改车管所
        $('#update').on('click', function () {
            var checkStatus = table.checkStatus('testReload'), data = checkStatus.data;
            if (data.length > 1) {
                layer.close(index);
                layer.alert("最多选择一条数据");
                return;
            }
            top.layer.open({
                title: "修改车管所",
                type: 2,
                fixed: true, //不固定
                maxmin: true,
                area: ['80%', '80%'],
                content: './aos/division/divisionUpdate.html?cityId=' + data[0].cityId
            });
        });

        $('#searchBtn').on('click', function () {
            var type = $(this).data('type');
            division[type] ? division[type].call(this) : '';
        });

    });

    //重置页面
    function reset() {
        //执行重载
        table.reload('testReload', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {}
        });
    }

</script>

</html>